<?php
declare (strict_types=1);

namespace app\controller\backend;

use app\BaseController;
use app\service\logic\backend\system\MenuService;
use app\service\logic\backend\system\PermissionsService;
use think\facade\View;

class BackendController extends BaseController
{
    /**
     * 渲染layout && js 数据
     *
     * @param $template
     * @param string $frontEnd
     * @return string
     */
    public function layout($template, $frontEnd = '')
    {
        $admin = $this->request->middleware('admin');
        // 获取当前系统菜单
        $menu = invoke(MenuService::class)->getListByPermissions($admin);
        if ($this->request->isGet()) {
            View::assign('title', lang('backend.common view title'));
            View::assign('name', $admin['name']);
            View::assign('avatar', $admin['avatar']);
            View::assign('menu', json_encode($menu, JSON_UNESCAPED_UNICODE));
            return View::fetch('backend/public/layout');
        }
        View::config(['view_suffix' => 'js']);

        if (!empty($frontEnd)) {
            // $frontEnds = invoke(PermissionsService::class)->getFrontEndByAuth(); // debug 阶段注释 权限相关
            // if (!in_array($frontEnd, $frontEnds)) {
            //     return View::fetch('backend/system/403');
            // }
        }

        return View::fetch($template);
    }

    /**
     * 简单兼容一下
     *
     * @param $template
     * @return string
     */
    public function layoutWithoutMenu($template)
    {
        if ($this->request->isGet()) {
            $admin = $this->request->middleware('admin');
            View::assign('title', lang('backend.background management system'));
            View::assign('name', $admin['name']);
            View::assign('avatar', $admin['avatar']);
            View::assign('menu', json_encode([], JSON_UNESCAPED_UNICODE));
            return View::fetch('backend/public/layout');
        }
        View::config(['view_suffix' => 'js']);
        return View::fetch($template);
    }
}
